<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="来源渠道" prop="source" label-width="90">
        <el-select
          v-model="queryParams.source"
          placeholder="请输入来源渠道"
          clearable
          class="!w-240px"
        >
          <el-option label="电话" value="1" />
          <el-option label="邮件" value="2" />
          <el-option label="系统上报" value="3" />
          <el-option label="APP" value="4" />
          <el-option label="微信" value="5" />
        </el-select>
      </el-form-item>
      <el-form-item label="当前状态" prop="status" label-width="90">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择当前状态"
          clearable
          class="!w-240px"
        >
          <el-option label="待分配" value="1" />
          <el-option label="已分配" value="2" />
          <el-option label="处理中" value="3" />
          <el-option label="待确认" value="4" />
          <el-option label="已完成" value="5" />
          <el-option label="已关闭" value="6" />
          <el-option label="已取消" value="7" />
        </el-select>
      </el-form-item>
      <el-form-item label="优先级" prop="priority" label-width="90">
        <el-select
          v-model="queryParams.priority"
          placeholder="请选择优先级"
          clearable
          class="!w-240px"
        >
          <el-option label="P0" value="P0" />
          <el-option label="P1" value="P1" />
          <el-option label="P2" value="P2" />
          <el-option label="P3" value="P3" />
          <el-option label="P4" value="P4" />
        </el-select>
      </el-form-item>

      <el-form-item label="故障级别" prop="severity" label-width="90">
        <el-select
          v-model="queryParams.severity"
          placeholder="请选择故障级别"
          clearable
          class="!w-240px"
        >
          <el-option label="致命" value="1" />
          <el-option label="严重" value="2" />
          <el-option label="一般" value="3" />
          <el-option label="轻微" value="4" />
        </el-select>
      </el-form-item>

      <el-form-item label="主要工程师" prop="primaryEngineer" label-width="90">
        <el-input
          v-model="queryParams.primaryEngineer"
          placeholder="请输入主要工程师"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime" label-width="90">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"
          ><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button
        >
        <el-button @click="resetQuery"
          ><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button
        >
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['health:robot-operation:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['health:robot-operation:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
    >
      <el-table-column label="序号" align="center" type="index" width="60px" />
      <el-table-column
        label="来源渠道"
        align="center"
        prop="source"
        width="100px"
      >
        <template #default="{ row }">
          {{ getSource(row.source) }}
        </template>
      </el-table-column>
      <el-table-column
        label="机器人类型"
        align="center"
        prop="robotType"
        width="100px"
      >
        <template #default="{ row }">
          {{ getrobotType(row.robotType) }}
        </template>
      </el-table-column>
      <el-table-column label="机器人编号" align="center" prop="robotCode" />
      <el-table-column label="机器人名称" align="center" prop="robotName" />
      <el-table-column
        label="当前状态"
        align="center"
        prop="status"
        width="100px"
      >
        <template #default="{ row }">
          {{ getstatus(row.status) }}
        </template>
      </el-table-column>
      <el-table-column
        label="优先级"
        align="center"
        prop="priority"
        width="100px"
      >
        <template #default="{ row }">
          {{ getpriority(row.priority) }}
        </template>
      </el-table-column>
      <el-table-column label="产品型号" align="center" prop="productModel" />
      <el-table-column label="故障代码" align="center" prop="errorCode" />
      <el-table-column label="故障级别" align="center" prop="severity">
        <template #default="{ row }">
          {{ geterrorCode(row.errorCode) }}
        </template>
      </el-table-column>
      <el-table-column
        label="故障发生时间"
        align="center"
        prop="failureTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="主要工程师"
        align="center"
        prop="primaryEngineer"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['health:robot-operation:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['health:robot-operation:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <RobotOperationForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from "@/utils/formatTime";
import download from "@/utils/download";
import {
  RobotOperationApi,
  RobotOperationVO,
} from "@/api/health/robotoperation";
import RobotOperationForm from "./RobotOperationForm.vue";

/** 运维工单 列表 */
defineOptions({ name: "RobotOperation" });

const message = useMessage(); // 消息弹窗
const { t } = useI18n(); // 国际化

const loading = ref(true); // 列表的加载中
const list = ref<RobotOperationVO[]>([]); // 列表的数据
const total = ref(0); // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  libraryId: undefined,
  source: undefined,
  robotType: undefined,
  robotId: undefined,
  robotCode: undefined,
  robotName: undefined,
  status: undefined,
  priority: undefined,
  productModel: undefined,
  errorCode: undefined,
  severity: undefined,
  failureTime: [],
  primaryEngineer: undefined,
  createTime: [],
});
const queryFormRef = ref(); // 搜索的表单
const exportLoading = ref(false); // 导出的加载中
const getSource = (source) => {
  const sourceMap = {
    1: "电话",
    2: "邮件",
    3: "系统上报",
    4: "APP",
    5: "微信",
  };
  return sourceMap[source] || "";
};
const getstatus = (row) => {
  const sourceMap = {
    1: "待分配",
    2: "已分配",
    3: "处理中",
    4: "待确认",
    5: "已完成",
    6: "已关闭",
    7: "已取消",
  };
  return sourceMap[row] || "";
};
const getpriority = (source) => {
  const sourceMap = {
    P0: "P0",
    P1: "P1",
    P2: "P2",
    P3: "P3",
    P4: "P4",
  };
  return sourceMap[source] || "";
};
const getrobotType = (row) => {
  const sourceMap = {
    1: "智能盘点机器人",
    2: "智能还书系统",
    3: "智能导览机器人",
  };
  return sourceMap[row] || "";
};
const geterrorCode = (errorCode) => {
  const errorCodeMap = {
    1: "致命",
    2: "严重",
    3: "一般",
    4: "轻微",
  };
  return errorCodeMap[errorCode] || "";
};
/** 查询列表 */
const getList = async () => {
  loading.value = true;
  try {
    const data = await RobotOperationApi.getRobotOperationPage(queryParams);
    list.value = data.list;
    total.value = data.total;
  } finally {
    loading.value = false;
  }
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields();
  handleQuery();
};

/** 添加/修改操作 */
const formRef = ref();
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id);
};

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm();
    // 发起删除
    await RobotOperationApi.deleteRobotOperation(id);
    message.success(t("common.delSuccess"));
    // 刷新列表
    await getList();
  } catch {}
};

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm();
    // 发起导出
    exportLoading.value = true;
    const data = await RobotOperationApi.exportRobotOperation(queryParams);
    download.excel(data, "运维工单.xls");
  } catch {
  } finally {
    exportLoading.value = false;
  }
};

/** 初始化 **/
onMounted(() => {
  getList();
});
</script>
